import type { Ref } from 'vue';

import { vectorServiceApi } from '#/api';

const getUniqueValues = async (
  layer: Ref<any>,
  field: string,
  order: 'asc' | 'desc' = 'asc',
): Promise<any[]> => {
  let fieldUniqueValues: any[] = [];

  try {
    fieldUniqueValues = await (Object.hasOwn(layer.value, 'source-layer')
      ? vectorServiceApi.getUniqueValues(
          layer.value['source-layer'],
          field,
          order,
        )
      : vectorServiceApi.getUniqueValuesFromGeojson(
          layer.value.source,
          field,
          order,
        ));
  } catch (error) {
    console.error('Error fetching unique values:', error);
  }
  return fieldUniqueValues;
};
export const getSingleValueRenderParams = async (
  templateParams: any,
  currentLayer: Ref<any>,
) => {
  const singleValueRenderParams = {} as any;
  singleValueRenderParams.layer = currentLayer.value.id;
  singleValueRenderParams.type = currentLayer.value.type;
  if (currentLayer.value.type === 'circle') {
    singleValueRenderParams.point = templateParams;
  } else if (currentLayer.value.type === 'line') {
    singleValueRenderParams.line = templateParams;
  } else {
    singleValueRenderParams.polygon = templateParams;
  }
  return singleValueRenderParams;
};
export const getUniqueValueRenderParams = async (
  templateParams: any,
  currentLayer: Ref<any>,
) => {
  const uniqueValueRenderParams = {} as any;
  uniqueValueRenderParams.layer = currentLayer.value.id;
  uniqueValueRenderParams.type = currentLayer.value.type;
  uniqueValueRenderParams.renderField = templateParams.renderField;
  uniqueValueRenderParams.uniqueValues = await getUniqueValues(
    currentLayer,
    templateParams.renderField,
  );
  uniqueValueRenderParams.colorRamp = templateParams.colorRamp;

  if (templateParams.colorRamp === 'city_land') {
    uniqueValueRenderParams.currentLegendList = [
      { uniqueValue: '工业用地', color: 'rgb(127,95,63)' },
      { uniqueValue: '公园与绿地用地', color: 'rgb(0,255,0)' },
      { uniqueValue: '机场设施用地', color: 'rgb(102,102,102)' },
      { uniqueValue: '交通场站用地', color: 'rgb(128,128,128)' },
      { uniqueValue: '教育科研用地', color: 'rgb(255,127,255)' },
      { uniqueValue: '居住用地', color: 'rgb(255,255,0)' },
      { uniqueValue: '商务办公用地', color: 'rgb(255,127,127)' },
      { uniqueValue: '商业服务用地', color: 'rgb(255,0,0)' },
      { uniqueValue: '体育与文化用地', color: 'rgb(0,165,41)' },
      { uniqueValue: '行政办公用地', color: 'rgb(255,0,255)' },
      { uniqueValue: '医疗卫生用地', color: 'rgb(255,0,63)' },
    ];
  }

  if (currentLayer.value.type === 'circle') {
    uniqueValueRenderParams.point = templateParams.renderParam;
  } else if (currentLayer.value.type === 'line') {
    uniqueValueRenderParams.line = templateParams.renderParam;
  } else {
    uniqueValueRenderParams.polygon = templateParams.renderParam;
  }
  return uniqueValueRenderParams;
};
export const getHierarchicalRenderParams = async (
  templateParams: any,
  currentLayer: Ref<any>,
) => {
  const hierarchicalRenderParams = {} as any;

  hierarchicalRenderParams.layer = currentLayer.value.id;
  hierarchicalRenderParams.type = currentLayer.value.type;
  hierarchicalRenderParams.uniqueValues = await getUniqueValues(
    currentLayer,
    templateParams.renderField,
  );

  Object.keys(templateParams).forEach((key) => {
    if (key !== 'renderParam') {
      hierarchicalRenderParams[key] = templateParams[key];
    }
  });

  if (currentLayer.value.type === 'circle') {
    hierarchicalRenderParams.point = templateParams.renderParam;
  } else if (currentLayer.value.type === 'line') {
    hierarchicalRenderParams.line = templateParams.renderParam;
  } else {
    hierarchicalRenderParams.polygon = templateParams.renderParam;
  }

  return hierarchicalRenderParams;
};

export const getGradualValueRenderParams = async (
  templateParams: any,
  currentLayer: Ref<any>,
) => {
  const gradualValueRenderParams = {} as any;
  gradualValueRenderParams.layer = currentLayer.value.id;
  gradualValueRenderParams.type = currentLayer.value.type;
  gradualValueRenderParams.uniqueValues = await getUniqueValues(
    currentLayer,
    templateParams.renderField,
    templateParams.order,
  );

  Object.keys(templateParams).forEach((key) => {
    if (key !== 'renderParam') {
      gradualValueRenderParams[key] = templateParams[key];
    }
  });

  if (currentLayer.value.type === 'circle') {
    gradualValueRenderParams.point = templateParams.renderParam;
  } else if (currentLayer.value.type === 'line') {
    gradualValueRenderParams.line = templateParams.renderParam;
  } else {
    gradualValueRenderParams.polygon = templateParams.renderParam;
  }
  return gradualValueRenderParams;
};

export const getHeatmapParams = async (
  templateParams: any,
  currentLayer: Ref<any>,
) => {
  const heatmapOptions = {} as any;
  heatmapOptions.layer = currentLayer.value;
  heatmapOptions.colorRamp = templateParams.colorRamp;
  heatmapOptions.weightField = templateParams.weightField;

  heatmapOptions['heatmap-weight'] = templateParams['heatmap-weight'];
  heatmapOptions.keepingLayer = templateParams.keepingLayer;
  heatmapOptions.layerOptions = {
    layout: {
      visibility: 'visible',
    },
    paint: templateParams.paint,
  };
  heatmapOptions.basedZoomOptions = {
    interpolateMethod: 'linear',
    zoomRange: templateParams.zoomRange,
  };
  // 如果原先有字段值，先获取到字段值的最大值
  if (templateParams.weightField) {
    const uniqueValues = await getUniqueValues(
      currentLayer,
      templateParams.weightField,
      'desc',
    );
    heatmapOptions.maxFieldVal = uniqueValues[0];
  }
  return heatmapOptions;
};

export const getTextLabelParams = (
  templateParams: any,
  currentLayer: Ref<any>,
) => {
  const textLabelOptions = {} as any;
  textLabelOptions.layer = currentLayer.value;
  textLabelOptions.renderField = templateParams.renderField;
  textLabelOptions.layout = templateParams.renderParam.layout;
  textLabelOptions.paint = templateParams.renderParam.paint;
  return textLabelOptions;
};

export const getSingleBandRenderParams = async (
  templateParams: any,
  currentLayer: Ref<any>,
) => {
  const singleBandRasterOptions = {} as any;

  singleBandRasterOptions.layer = currentLayer.value;
  singleBandRasterOptions.layout = {
    visibility: 'visible',
  };
  Object.keys(templateParams).forEach((key) => {
    singleBandRasterOptions[key] = templateParams[key];
  });
  return singleBandRasterOptions;
};
